<script setup lang="ts">
import { request } from '@/api'
import { closeToast, showLoadingToast, type ActionSheetAction } from 'vant'
import { onMounted, ref } from 'vue'
// import { areaList } from '@vant/area-data'

// 表单变量
const profile = ref<ProfileInfo>({} as ProfileInfo)

const avatarSrc = ref(
  'https://i2.hdslb.com/bfs/face/65d26cc9e6a628d38edc672f73d4652e6919e6f3.jpg',
)

// 弹框是否显示
const showAvatarSheet = ref(false)
const showDatePicker = ref(false)
const showLocationPicker = ref(false)
const showProfessionPicker = ref(false)

// 头像选择 弹框数据
const avatarActions: ActionSheetAction[] = [
  {
    name: '拍照',
    icon: 'camera',
    callback: async () => {
      showAvatarSheet.value = false
      const res = await mk.pickerCamera()
      console.log(res)
    },
  },
  {
    name: '相册',
    icon: 'photo',
    callback: async () => {
      showAvatarSheet.value = false
      mk.ppt('唤起相册中~')
      const res = await mk.pickerPhoto()
      // console.log(res)
      avatarSrc.value = res
    },
  },
]
// 日期选择器 回调函数
const handleDatePickerConfirm = (data: { selectedValues: string[] }) => {
  profile.value.birthday =
    data.selectedValues[0] +
    '-' +
    data.selectedValues[1] +
    '-' +
    data.selectedValues[2]
  console.log(profile.value.birthday)
  showDatePicker.value = false
}
// 日期选择器 默认日期
const currentDate = ref(['2024', '10', '23'])

// 所在地选择 数据变量准备
const areaList = ref({
  province_list: {},
  city_list: {},
  county_list: {},
})

// 获取地区数据
const fetchAreaList = async () => {
  const result = await mk.getAreaColumns()
  areaList.value = result
}

// 网络请求获取用户信息
const fetchUserInfo = async () => {
  showLoadingToast({ message: '加载中...', duration: 0, forbidClick: true })
  const result = await request.get<MkResponse<ProfileInfo>>('member/profile')
  profile.value = result.data.result
  currentDate.value = profile.value.birthday.split('-')
  console.log(profile.value)
  closeToast()
}

// 在组件挂载时获取地区数据
onMounted(() => {
  fetchAreaList()
  fetchUserInfo()
})

// 处理地区选择确认
const handleLocationPickerConfirm = (data: {
  selectedOptions: { text: string }[]
}) => {
  showLocationPicker.value = false
  // console.log(data)
  profile.value.fullLocation = data.selectedOptions
    .map(option => option.text)
    .join(' ')
}

// 职业数据
const professionList = [
  // text 呈现给用户看，value 提交给后端用
  { text: '软件工程师', value: '软件工程师' },
  { text: '医生', value: '医生' },
  { text: '教师', value: '教师' },
  { text: '律师', value: '律师' },
  { text: '会计师', value: '会计师' },
  { text: '销售经理', value: '销售经理' },
  { text: '市场营销专员', value: '市场营销专员' },
  { text: '建筑师', value: '建筑师' },
  { text: '护士', value: '护士' },
  { text: '机械工程师', value: '机械工程师' },
]

// 职业选择器 回调函数
const handleProfessionPickerConfirm = (data: {
  selectedOptions: { text: string }[]
}) => {
  showProfessionPicker.value = false
  profile.value.profession = data.selectedOptions
    .map(option => option.text)
    .join(' ')
}

// 保存资料
const handleSubmit = () => {
  mk.ppt('保存资料中~')
  mk.sayHi(profile.value)
}
</script>

<template>
  <div class="profile-edit-page">
    <!-- 头像部分 -->
    <div class="avatar">
      <van-image
        round
        width="100"
        height="100"
        class="avatar-img"
        :src="profile.avatar"
        @click="showAvatarSheet = true"
      >
      </van-image>
      <div class="avatar-btn">
        <span @click="showAvatarSheet = true">修改头像</span>
      </div>
    </div>

    <!-- 切换头像 action-sheet -->
    <van-action-sheet
      v-model:show="showAvatarSheet"
      :actions="avatarActions"
      cancel-text="取消"
    />
    <!-- 生日选择器 -->
    <van-popup v-model:show="showDatePicker" position="bottom" round>
      <van-date-picker
        title="选择出生日期"
        :min-date="new Date(2000, 0, 1)"
        :max-date="new Date()"
        v-model="currentDate"
        @cancel="showDatePicker = false"
        @confirm="handleDatePickerConfirm"
      />
    </van-popup>
    <!-- 所在地选择器 -->
    <van-popup v-model:show="showLocationPicker" position="bottom" round>
      <van-area
        title="选择所在地区"
        :area-list="areaList"
        @confirm="handleLocationPickerConfirm"
        @cancel="showLocationPicker = false"
      />
    </van-popup>
    <!-- 职业选择器 -->
    <van-popup v-model:show="showProfessionPicker" position="bottom" round>
      <van-picker
        title="选择职业"
        :columns="professionList"
        @confirm="handleProfessionPickerConfirm"
        @cancel="showProfessionPicker = false"
      />
    </van-popup>

    <!-- 中间表单部分 -->
    <van-cell-group>
      <van-field label="账号" v-model="profile.account" readonly></van-field>
      <van-field
        label="昵称"
        placeholder="请输入昵称"
        v-model="profile.nickname"
      ></van-field>
      <van-cell title="性别" class="gender">
        <van-radio-group v-model="profile.gender" :icon-size="16">
          <van-radio name="男">男</van-radio>
          <van-radio name="女">女</van-radio>
          <van-radio name="未知">未知</van-radio>
        </van-radio-group>
      </van-cell>
      <van-field
        label="生日"
        readonly
        placeholder="请选择日期"
        @click="showDatePicker = true"
        v-model="profile.birthday"
      ></van-field>
      <van-field
        label="所在地"
        readonly
        placeholder="请选择所在地"
        v-model="profile.fullLocation"
        @click="showLocationPicker = true"
      ></van-field>
      <van-field
        label="职业"
        readonly
        placeholder="请选择职业"
        v-model="profile.profession"
        @click="showProfessionPicker = true"
      ></van-field>
    </van-cell-group>

    <div class="submit">
      <van-button round block type="primary" @click="handleSubmit">
        保存资料
      </van-button>
    </div>
  </div>
</template>

<style lang="css" scoped>
.avatar {
  padding: 30px;
  text-align: center;
  background-color: var(--mk-white);
}

.avatar-img {
  box-shadow: 0 0 5px var(--mk-gray);
}

.avatar-btn {
  color: var(--mk-linear_end);
  margin-top: 10px;
}

.gender ::v-deep(.van-cell__title) {
  width: 100px;
  flex: none;
}

.van-radio-group {
  display: flex;
  justify-content: space-between;
  height: 36px;
  width: 180px;
}

.submit {
  padding: 16px;
}
</style>
